package com.ui.design.view.gradualcolor.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import com.ui.design.R;
import com.ui.design.utils.DensityUtil;

import java.util.List;

/**
 * 渐变色控件，可设置四色位置
 */
public class GradualColor extends View {
    private int colorStart;
    private int colorEnd;
    private int colorAfterStart;
    private int colorBeforeEnd;
    private float round;
    private RectF mBackGroundRect;
    private LinearGradient backGradient;
    //默认画笔
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint mPaintText = new Paint();
    List<Integer> colorlist;

    private ColorWay way=ColorWay.HORIZONTAL;
    private int[] Colors={};

    public GradualColor(Context context, AttributeSet attrs) {
        super(context, attrs);
        //获取自定义属性（可先设置默认四种颜色）
        colorStart = Color.parseColor("#FFDDBB");
        colorAfterStart = Color.parseColor("#FFC0CD");
        colorBeforeEnd = Color.parseColor("#B5BBFF");
        colorEnd = Color.parseColor("#AADCFF");
        //也可通过attr进行xml文件内的自定义设置
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyGradient);
            colorStart = typedArray.getColor(R.styleable.MyGradient_GradientcolorStart, colorStart);
            colorEnd = typedArray.getColor(R.styleable.MyGradient_GradientcolorEnd, colorEnd);
            colorAfterStart = typedArray.getColor(R.styleable.MyGradient_GradientcolorafterStart, colorAfterStart);
            colorBeforeEnd = typedArray.getColor(R.styleable.MyGradient_GradientcolorbeforeEnd, colorBeforeEnd);
            round = typedArray.getDimension(R.styleable.MyGradient_Gradientround, DensityUtil.dip2px(context, 10));
        }
        //设置抗锯齿
        mPaint.setAntiAlias(true);
        //设置防抖动
        mPaint.setDither(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaintText.setAntiAlias(true);
        mPaintText.setDither(true);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mBackGroundRect = new RectF(0, 0, w, h);
        Colors=new int[]{colorStart, colorAfterStart, colorBeforeEnd, colorEnd};

    }

    /**
     * 设置更多颜色
     * 通过跳转w/h比例改变渐变方向
     * @param colors 颜色组
     * @param colorWay 渲染方向
     *
     * 水平  HORIZONTAL
     * 垂直  VERTICAL
     * 斜  OBLIQUE
     */
    public void SetMoreColor(int[] colors,ColorWay colorWay) {
        this.way=colorWay;
        Colors=colors;
        backGradient= null;
        invalidate();
    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (backGradient==null){
            switch (way){
                case OBLIQUE:
                    backGradient = new LinearGradient(0, 0, getMeasuredWidth(), getMeasuredHeight(),Colors, null, Shader.TileMode.CLAMP);
                    break;
                case VERTICAL:
                    backGradient = new LinearGradient(0, 0, 0, getMeasuredHeight(),Colors, null, Shader.TileMode.CLAMP);
                    break;
                case HORIZONTAL:
                    backGradient = new LinearGradient(0, 0, getMeasuredWidth(),0,Colors, null, Shader.TileMode.CLAMP);
                    break;

            }
        }
        mPaint.setShader(backGradient);
        //绘制背景 圆角矩形
        if (mBackGroundRect != null) {
            canvas.drawRoundRect(mBackGroundRect, round, round, mPaint);
        }
    }

    /**
     * 水平
     * 垂直
     * 斜
     */
   public enum ColorWay{
        HORIZONTAL,
        VERTICAL,
        OBLIQUE
    }
}
